
package com.gw.business.system.login.controller;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.gw.business.common.constant.GlobalConstant;
import com.gw.business.common.util.ValidateCode;
import com.gw.business.system.menu.service.ISystemService;
import com.gw.platform.controller.DefaultController;
import com.gw.platform.model.UserModel;

/**
 * 类功能:处理用户登录请求控制器
 * <p>
 * 创建者:关博
 * </p>
 * <p>
 * 创建时间:2014-06-20
 * </p>
 * <p>
 * 修改者:
 * </p>
 * <p>
 * 修改时间:
 * </p>
 * <p>
 * 修改原因：
 * </p>
 * <p>
 * 审核者:
 * </p>
 * <p>
 * 审核时间:
 * </p>
 * <p>
 * 审核意见：
 * </p>
 */
@Controller
@RequestMapping("system")
public class LoginController extends DefaultController {
    
    @Resource
    private ISystemService iSystemService;
    
    /**
     * 登陆请求验证
     * 
     * @param redirectAttributes
     * @param user
     * @return
     */
    @RequestMapping("/login")
    public Callable<String> login(final RedirectAttributes redirectAttributes, final UserModel user) {
        return new Callable<String>() {
            
            public String call() throws Exception {
                if (user.getUserName() == null || user.getPassWord() == null) {
                    //redirectAttributes.addFlashAttribute(GlobalConstant.ERROR_MESSAGE, "用户名或密码输入错误");
                    return "redirect:/";
                }
                
                Map map = (Map) defaultService.load("sysUser.user_load", user);
                
                if (map == null) {
                    redirectAttributes.addFlashAttribute(GlobalConstant.ERROR_MESSAGE, "用户名或密码输入错误");
                    return "redirect:/";
                }
                String authCode = request.getParameter("authCode");
                
                // 验证码
                String randNum = (String) session.getAttribute("randCode");
                if (null == authCode) {
                    
                    redirectAttributes.addFlashAttribute(GlobalConstant.ERROR_MESSAGE, "验证码不能为空");
                    return "redirect:/";
                } else {
                    if (!authCode.endsWith(randNum)) {
                        redirectAttributes.addFlashAttribute(GlobalConstant.ERROR_MESSAGE, "验证码错误");
                        return "redirect:/";
                    }
                    
                }
                user.setPassWord((String) map.get("passwrd"));
                user.setUserName((String) map.get("username"));
                user.setId((Integer) map.get("id"));
                
                GlobalConstant.SYSTEM_MENU = iSystemService.getMeunTree("SystemMenu.System_loadMenu", user.getId());
                session.setAttribute("systemMenu", GlobalConstant.SYSTEM_MENU);
                // 设置session用户信息
                session.setAttribute(GlobalConstant.SESSION_USER, user);
                
                return "system/main";
            }
        };
    }
    
    /**
     * 获取验证码
     * 
     * @param request 请求
     * @param response 响应
     */
    @RequestMapping("/getAuthCode")
    public void getAuthCode(HttpSession session, HttpServletResponse response) {
        // 设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        String verifyCode = ValidateCode.generateTextCode(ValidateCode.TYPE_NUM_ONLY, 4, null);
        session.setAttribute("randCode", verifyCode);
        response.setContentType("image/jpeg");
        BufferedImage bim = ValidateCode.generateImageCode(verifyCode, 90, 30, 3, true, Color.WHITE, Color.BLACK, null);
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ImageIO.write(bim, "JPEG", outputStream);
            outputStream.flush();
        } catch (IOException e) {
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }
    
    /**
     * 系统菜单页
     * 
     * @return
     */
    @RequestMapping("/menu")
    public String menu() {
        return "system/menu";
    }
    
    /**
     * 系统头页
     * 
     * @return
     */
    @RequestMapping("/top")
    public String top() {
        return "system/top";
    }
    
    /**
     * 底部页面
     * 
     * @return
     */
    @RequestMapping("/bottom")
    public String bottom() {
        return "system/bottom";
    }
    
    @RequestMapping("/right")
    public String right() {
        return "system/right";
    }
    
    /**
     * 退出
     * 
     * @return
     */
    @RequestMapping("/logout")
    public Callable<String> logout() {
        return new Callable<String>() {
            
            public String call() throws Exception {
                session.invalidate();
                return "system/login";
            }
        };
    }
}
